package com.example.isometric_tower_defense;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class GamePanel extends JPanel implements ActionListener {
    public static final int TILE_WIDTH = 64;
    public static final int TILE_HEIGHT = 32;
    private static final int MAP_WIDTH = 15;
    private static final int MAP_HEIGHT = 15;
    
    // 游戏对象类型
    public static final int EMPTY = 0;
    public static final int GRASS = 1;
    public static final int PATH = 2;
    public static final int TOWER_SPOT = 3;
    
    // 地图数据
    private int[][] map;
    
    // 视角偏移
    private int cameraX = 0;
    private int cameraY = 0;
    
    // 敌人列表
    private List<Enemy> enemies;
    
    // 防御塔列表
    private List<Tower> towers;
    
    // 路径点
    private List<Point> pathPoints;
    
    // 游戏时钟
    private Timer gameTimer;
    
    public GamePanel() {
        initPath();
        initMap();
        initEnemies();
        initTowers();
        initGameTimer();
    }
    
    private void initPath() {
        pathPoints = new ArrayList<>();
        // 创建Z字形路径点
        // 从左到右的水平路径（y=7）
        for (int x = 0; x < MAP_WIDTH; x++) {
            pathPoints.add(new Point(x, 7));
        }
        // 从上到下的垂直路径（x=14）
        for (int y = 8; y < MAP_HEIGHT; y++) {
            pathPoints.add(new Point(MAP_WIDTH-1, y));
        }
        // 从右到左的水平路径（y=14）
        for (int x = MAP_WIDTH-2; x >= 0; x--) {
            pathPoints.add(new Point(x, MAP_HEIGHT-1));
        }
    }
    
    private void initMap() {
        map = new int[MAP_HEIGHT][MAP_WIDTH];
        // 初始化地图数据
        for (int y = 0; y < MAP_HEIGHT; y++) {
            for (int x = 0; x < MAP_WIDTH; x++) {
                // 默认为草地
                map[y][x] = GRASS;
            }
        }
        
        // 标记路径点
        for (Point p : pathPoints) {
            map[p.y][p.x] = PATH;
        }
        
        // 设置塔点
        for (int y = 5; y < MAP_HEIGHT-2; y++) {
            if (y % 2 == 1) {
                map[y][3] = TOWER_SPOT;
            }
        }
        for (int y = 1; y < MAP_HEIGHT-6; y++) {
            if (y % 2 == 1) {
                map[y][MAP_WIDTH-4] = TOWER_SPOT;
            }
        }
    }
    
    private void initEnemies() {
        enemies = new ArrayList<>();
        // 添加一些敌人
        Enemy enemy1 = new Enemy(pathPoints);
        enemy1.setSpeed(1); // 设置较慢的速度
        enemies.add(enemy1);
        
        Enemy enemy2 = new Enemy(pathPoints);
        enemy2.setSpeed(1); // 设置较慢的速度
        enemy2.x = pathPoints.get(0).x - 10; // 初始位置稍微靠后
        enemy2.y = pathPoints.get(0).y;
        enemies.add(enemy2);
        
        Enemy enemy3 = new Enemy(pathPoints);
        enemy3.setSpeed(1); // 设置较慢的速度
        enemy3.x = pathPoints.get(0).x - 20; // 初始位置更靠后
        enemy3.y = pathPoints.get(0).y;
        enemies.add(enemy3);
    }
    
    private void initTowers() {
        towers = new ArrayList<>();
        // 在塔点添加一些塔
        towers.add(new Tower(3, 6));
        towers.add(new Tower(MAP_WIDTH-4, 5));
    }
    
    private void initGameTimer() {
        gameTimer = new Timer(16, this); // 约60 FPS
        gameTimer.start();
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        Graphics2D g2d = (Graphics2D) g.create();
        
        // 绘制等距地图
        renderIsometricMap(g2d);
        
        // 渲染敌人
        renderEnemies(g2d);
        
        // 渲染防御塔
        renderTowers(g2d);
        
        g2d.dispose();
    }
    
    private void renderIsometricMap(Graphics2D g) {
        // 等距地图渲染逻辑
        for (int y = 0; y < MAP_HEIGHT; y++) {
            for (int x = 0; x < MAP_WIDTH; x++) {
                // 转换为等距坐标
                int isoX = (x - y) * TILE_WIDTH / 2;
                int isoY = (x + y) * TILE_HEIGHT / 2;
                
                // 应用摄像机偏移
                isoX -= cameraX;
                isoY -= cameraY;
                
                // 屏幕中心对齐
                isoX += getWidth() / 2;
                isoY += getHeight() / 4;
                
                // 绘制地块
                drawTile(g, isoX, isoY, map[y][x]);
            }
        }
    }
    
    private void renderEnemies(Graphics2D g) {
        // 渲染所有敌人
        for (Enemy enemy : enemies) {
            // 将敌人坐标转换为等距坐标
            int isoX = (enemy.x - enemy.y) * TILE_WIDTH / 2;
            int isoY = (enemy.x + enemy.y) * TILE_HEIGHT / 2;
            
            // 应用摄像机偏移
            isoX -= cameraX;
            isoY -= cameraY;
            
            // 屏幕中心对齐
            isoX += getWidth() / 2;
            isoY += getHeight() / 4;
            
            // 绘制敌人
            enemy.render(g, isoX, isoY);
        }
    }
    
    private void renderTowers(Graphics2D g) {
        // 渲染所有防御塔
        for (Tower tower : towers) {
            // 将塔坐标转换为等距坐标
            int isoX = (tower.x - tower.y) * TILE_WIDTH / 2;
            int isoY = (tower.x + tower.y) * TILE_HEIGHT / 2;
            
            // 应用摄像机偏移
            isoX -= cameraX;
            isoY -= cameraY;
            
            // 屏幕中心对齐
            isoX += getWidth() / 2;
            isoY += getHeight() / 4;
            
            // 绘制防御塔
            tower.render(g, isoX, isoY);
        }
    }
    
    private void drawTile(Graphics2D g, int x, int y, int tileType) {
        // 根据不同类型使用不同颜色绘制地块
        switch (tileType) {
            case GRASS: // 草地 - 绿色
                g.setColor(Color.GREEN);
                break;
            case PATH: // 路径 - 棕色
                g.setColor(new Color(139, 69, 19)); // 棕色
                break;
            case TOWER_SPOT: // 塔点 - 黄色
                g.setColor(Color.YELLOW);
                break;
            case EMPTY:
            default:
                g.setColor(Color.WHITE);
                break;
        }
        
        // 绘制菱形地块
        int[] xPoints = {x, x + TILE_WIDTH/2, x, x - TILE_WIDTH/2};
        int[] yPoints = {y - TILE_HEIGHT/2, y, y + TILE_HEIGHT/2, y};
        g.fillPolygon(xPoints, yPoints, 4);
        
        // 绘制网格线
        g.setColor(Color.BLACK);
        g.drawPolygon(xPoints, yPoints, 4);
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        // 游戏主循环更新
        updateEnemies();
        updateTowers();
        
        repaint();
    }
    
    private void updateEnemies() {
        // 更新所有敌人
        for (Enemy enemy : enemies) {
            enemy.update();
        }
    }
    
    private void updateTowers() {
        // 更新所有防御塔
        for (Tower tower : towers) {
            tower.update();
            
            // 检查是否可以攻击敌人
            for (Enemy enemy : enemies) {
                if (tower.canAttack(enemy)) {
                    tower.attack(enemy);
                }
            }
        }
    }
}